<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let p1 = new Promise((resolve, reject) => { })
        console.log(p1);
        //自定义MyPromise类
        class MyPromise {
            constructor(handle) {
                this['[[PromiseState]]'] = 'pendding';
                this['[[PromiseResult]]'] = undefined;
                // handle(function (val) {//似外部的resolve方法
                //     console.log(this)
                //     //不bind时this是undefined，没法设置[[PromiseState]]，bind是改变指向但是不执行 
                //     this['[[PromiseState]]'] = 'fulfilled';
                //     this['[[PromiseResult]]'] = val;
                // }.bind(this), function (err) {//似外部的reject方法
                //     this['[[PromiseState]]'] = 'rejected';
                //     this['[[PromiseResult]]'] = err;
                //     throw new Error(err);

                // }.bind(this))
                handle(this.#_resolve.bind(this),this.#_reject.bind(this))
            }

            #_resolve(val) { // 私有方法 需要在前面加上#号，只能类的内部调用外部不能调用
                this['[[PromiseState]]'] = 'fulfilled';
                this['[[PromiseResult]]'] = val;
            }

            #_reject(val) {
                this['[[PromiseState]]'] = 'rejected';
                this['[[PromiseResult]]'] = err;
                throw new Error(err);
            }
        }

        let p2 = new MyPromise((resolve, reject) => {
            // reject();
            resolve("成功");
            
        })
        console.log(p2)
    </script>
</body>

</html>